package qc.module.ehs.mapper;

import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsTaskCategoryTypeEnum;
import qc.common.core.enums.EhsTaskDestObjectTypeEnum;
import qc.common.core.enums.EhsTaskGroupObjectTypeEnum;
import qc.common.core.enums.TimeRangeLengthEnum;
import qc.common.core.utils.StringSplitUtil;
import qc.module.ehs.dto.task.TaskAddOrEditDto;
import qc.module.ehs.dto.task.TaskInfoDto;
import qc.module.ehs.entity.EhsTaskInfo;

@Mapper
public interface EhsTaskInfoMapper {
    EhsTaskInfoMapper MAPPER = Mappers.getMapper(EhsTaskInfoMapper.class);

    @Mapping(target = "intm", source = "intm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "planbegintm", source = "planbegintm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "planendtm", source = "planendtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "tasktype", source = "tasktype.index")
    @Mapping(target = "objtype", source = "objtype.index")
    @Mapping(target = "planfrequencytype", source = "planfrequencytype.index")
    @Mapping(target = "checktableids", ignore = true)
    TaskInfoDto toDto(EhsTaskInfo en);

    default TaskInfoDto entityToDto(EhsTaskInfo en) {
        TaskInfoDto dto = toDto(en);

        //检查表ID集合处理
        if (StringUtils.isNotBlank(en.getChecktableids())) {
            dto.setChecktableids(StringSplitUtil.Split(en.getChecktableids(), true));
        }

        return dto;
    }

    @Mapping(target = "planbegintm", ignore = true)//起止时间在外部转换并进行有效性判断
    @Mapping(target = "planendtm", ignore = true)
    @Mapping(target = "tasktype", ignore = true)
    @Mapping(target = "groupobjtype", ignore = true)
    @Mapping(target = "objtype", ignore = true)
    @Mapping(target = "planfrequencytype", ignore = true)
    @Mapping(target = "intm", ignore = true)
    @Mapping(target = "checktableids", ignore = true)
    EhsTaskInfo toEntity(TaskAddOrEditDto addDto);

    default EhsTaskInfo addOrEditDtoToEntity(TaskAddOrEditDto dto) {
        EhsTaskInfo en = EhsTaskInfoMapper.MAPPER.toEntity(dto);

        en.setTasktype(getEhsTaskCategoryTypeEnum(dto.getTasktype()));
        en.setPlanfrequencytype(getTimeRangeLengthEnum(dto.getPlanfrequencytype()));
        //任务分组类型
        en.setGroupobjtype(getEhsTaskGroupObjectTypeEnum(dto.getGroupobjtype()));
        en.setObjtype(getEhsTaskDestObjectTypeEnum(dto.getObjtype()));
        //检查表ID集合处理
        if (dto.getChecktableids() != null && dto.getChecktableids().length > 0x0) {
            en.setChecktableids(StringSplitUtil.Join(dto.getChecktableids()));
        }

        return en;
    }

    static EhsTaskCategoryTypeEnum getEhsTaskCategoryTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsTaskCategoryTypeEnum item : EhsTaskCategoryTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为null，任务大类必须有
        return null;
    }

    static EhsTaskGroupObjectTypeEnum getEhsTaskGroupObjectTypeEnum(Integer index) {
        if (index != null && index >= 0x0) {
            for (EhsTaskGroupObjectTypeEnum item : EhsTaskGroupObjectTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为null，任务分组对象类型必须有
        return null;
    }

    static EhsTaskDestObjectTypeEnum getEhsTaskDestObjectTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsTaskDestObjectTypeEnum item : EhsTaskDestObjectTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //任务目标对象类型默认为0--未定义
        return EhsTaskDestObjectTypeEnum.UNDEFINED;
    }

    static TimeRangeLengthEnum getTimeRangeLengthEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (TimeRangeLengthEnum item : TimeRangeLengthEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //任务的频次默认使用自定义，表示单次任务
        return TimeRangeLengthEnum.CUSTOM;
    }
}
